package com.ruoyi.web.controller.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.web.controller.demo.domain.SysSchedulPaiRelate;
import com.ruoyi.web.controller.mapper.SysSchedulZhiMapper;
import com.ruoyi.web.controller.demo.domain.SysSchedulZhi;
import com.ruoyi.web.controller.service.ISysSchedulZhiService;
import com.ruoyi.common.core.text.Convert;

/**
 * sys_schedul_zhiService业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-27
 */
@Service
public class SysSchedulZhiServiceImpl implements ISysSchedulZhiService 
{
    @Autowired
    private SysSchedulZhiMapper sysSchedulZhiMapper;

    /**
     * 查询sys_schedul_zhi
     * 
     * @param id sys_schedul_zhi主键
     * @return sys_schedul_zhi
     */
    @Override
    public SysSchedulZhi selectSysSchedulZhiById(Long id)
    {
        return sysSchedulZhiMapper.selectSysSchedulZhiById(id);
    }

    /**
     * 查询sys_schedul_zhi列表
     * 
     * @param sysSchedulZhi sys_schedul_zhi
     * @return sys_schedul_zhi
     */
    @Override
    public List<SysSchedulZhi> selectSysSchedulZhiList(SysSchedulZhi sysSchedulZhi)
    {
        return sysSchedulZhiMapper.selectSysSchedulZhiList(sysSchedulZhi);
    }

    /**
     * 新增sys_schedul_zhi
     * 
     * @param sysSchedulZhi sys_schedul_zhi
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysSchedulZhi(SysSchedulZhi sysSchedulZhi)
    {
        sysSchedulZhi.setCreateTime(DateUtils.getNowDate());
        int rows = sysSchedulZhiMapper.insertSysSchedulZhi(sysSchedulZhi);
        insertSysSchedulPaiRelate(sysSchedulZhi);
        return rows;
    }

    /**
     * 修改sys_schedul_zhi
     * 
     * @param sysSchedulZhi sys_schedul_zhi
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysSchedulZhi(SysSchedulZhi sysSchedulZhi)
    {
        sysSchedulZhiMapper.deleteSysSchedulPaiRelateByPaiId(sysSchedulZhi.getId());
        insertSysSchedulPaiRelate(sysSchedulZhi);
        return sysSchedulZhiMapper.updateSysSchedulZhi(sysSchedulZhi);
    }

    /**
     * 批量删除sys_schedul_zhi
     * 
     * @param ids 需要删除的sys_schedul_zhi主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysSchedulZhiByIds(String ids)
    {
        sysSchedulZhiMapper.deleteSysSchedulPaiRelateByPaiIds(Convert.toStrArray(ids));
        return sysSchedulZhiMapper.deleteSysSchedulZhiByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除sys_schedul_zhi信息
     * 
     * @param id sys_schedul_zhi主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysSchedulZhiById(Long id)
    {
        sysSchedulZhiMapper.deleteSysSchedulPaiRelateByPaiId(id);
        return sysSchedulZhiMapper.deleteSysSchedulZhiById(id);
    }

    /**
     * 新增schedul_pai_relate信息
     * 
     * @param sysSchedulZhi sys_schedul_zhi对象
     */
    public void insertSysSchedulPaiRelate(SysSchedulZhi sysSchedulZhi)
    {
        List<SysSchedulPaiRelate> sysSchedulPaiRelateList = sysSchedulZhi.getSysSchedulPaiRelateList();
        Long id = sysSchedulZhi.getId();
        if (StringUtils.isNotNull(sysSchedulPaiRelateList))
        {
            List<SysSchedulPaiRelate> list = new ArrayList<SysSchedulPaiRelate>();
            for (SysSchedulPaiRelate sysSchedulPaiRelate : sysSchedulPaiRelateList)
            {
                sysSchedulPaiRelate.setPaiId(id);
                list.add(sysSchedulPaiRelate);
            }
            if (list.size() > 0)
            {
                sysSchedulZhiMapper.batchSysSchedulPaiRelate(list);
            }
        }
    }
}
